import { resolveAddress } from "@/api/order";

// 关键词处理 - 形成key与value的数组
const keywordDispose = (keyword) => {
    return keyword.slice(keyword.indexOf("?") + 1).split("&").map((param) => param.split("=")).map(([key, value]) => ({ key, value }));
}

// 查询数组中某个关键词
const findKeyFormVal = (key, arr) => {
    return arr.find((el) => el.key === key).value;
}

export const searchByUrl = async (keyWords) => {

    let id = "", channel = "";
    if (keyWords.indexOf("detail.1688.com") != -1) {
        const n = keyWords.lastIndexOf("/");
        const result = keyWords.substring(n + 1);
        id = result?.split(".")[0];
        channel = "1688";
        return { id, channel };
    }

    if (keyWords.indexOf("//detail.m.1688.com") != -1) {
        //http://detail.m.1688.com/page/index.htm?offerId=899719435540
        const paramsArray = keyWords
            .slice(keyWords.indexOf("?") + 1)
            .split("&")
            .map((param) => param.split("="))
            .map(([key, value]) => ({ key, value }));
        id = paramsArray.find(
            (el) => el.key === "offerId"
        ).value;
        channel = "1688";
        return { id, channel };
    }
    if (keyWords.indexOf("//m.1688.com") != -1) {

        // https://m.1688.com/offer/713477816834.html?share_token=3LdI3LTGYBbirkwWY9JTrfFw5BOiOBdLH0mitK7Io2/WCM3ohDy5eQ==&__share_id=657555de-747d-44d4-b3c3-4c09f9551866&rpg-cnt=share.newdetail&offerId=713477816834&__zhi_token=FX7QbJp00exaq&spm=a262eq.8992535/20240905.5103953.0&callByHgJs=1&__removesafearea__=1&cbu_sv_id=967a4af4-d198-4f88-b75b-19ddd17d22ba&src_cna=8PJfH1rFb1ECAXAwOZDm1vcZ&visitorId=967a4af4-d198-4f88-b75b-19ddd17d22ba

        let url = keyWords;
        let quote = url.indexOf("?")
        if (quote != -1) {
            url = url.substring(0, quote);
        }
        const n = url.indexOf(".html");
        /*
        *	2024-12-02修改判断规则
        * html和?之间有可能会有 /_____tmd_____/punish 这样的字符串，并且这个是允许存在的
        * 规则调整
        * 	1.搜索第一个.html的所在位置，切割之前的字符串
        * 	2.获取最后一个/所在下标
        * 	3.获取下标+1之后的字符串，即id
        */
        const n1 = url.substring(0, n);
        const n2 = n1.lastIndexOf('/')
        const result = url.substring(n2 + 1);
        id = result?.split(".")[0];
        channel = "1688";
        return { id, channel };
    }
    if (
        keyWords.indexOf("taobao.com") != -1 ||
        keyWords.indexOf("tmall.com") != -1 ||
        keyWords.indexOf("tmall.hk") != -1
    ) {
        const url = keyWords;
        const paramsArray = url
            .slice(url.indexOf("?") + 1)
            .split("&")
            .map((param) => param.split("="))
            .map(([key, value]) => ({ key, value }));
        id = paramsArray.find((el) => el.key === "id").value;
        channel = "TAOBAO";
        return { id, channel };
    }
    if (keyWords.indexOf("weidian.com") != -1) {
        const url = keyWords;
        const paramsArray = url
            .slice(url.indexOf("?") + 1)
            .split("&")
            .map((param) => param.split("="))
            .map(([key, value]) => ({ key, value }));
        id = paramsArray.find(
            (el) => el.key === "itemId" || el.key === "itemID"
        ).value;
        channel = "weidian";
        return { id, channel };
    }

    if (keyWords.indexOf('100buy.com') != -1) {
        if (keyWords.indexOf('?') > 0) {
            keyWords = keyWords.split('?')[0];
        }
        let reg = /product\/(\d+)\/(\w+)/i
        let matches = keyWords.match(reg);
        if (matches.length == 3) {
            id = matches[2];
            channel = matches[1]
        }
        return { id, channel };
    }

    if (keyWords.indexOf('cnfans.com') != -1 || keyWords.indexOf('mulebuy.com') != -1) {
        const paramsArray = keyWords
            .slice(keyWords.indexOf("?") + 1)
            .split("&")
            .map((param) => param.split("="))
            .map(([key, value]) => ({ key, value }));
        id = paramsArray.find(
            (el) => el.key === "id"
        ).value;
        channel = paramsArray.find(
            (el) => el.key === "shop_type"
        ).value;

        if (channel == 'taobao') {
            channel = '1';
        } else if (channel == 'ali_1688') {
            channel = '0';
        } else if (channel == 'weidian') {
            channel = '2';
        }
        return { id, channel };
    }

    if (keyWords.indexOf('ponybuy.com') != -1) {
        let ponyArr = keywordDispose(keyWords);
        id = findKeyFormVal('product_id', ponyArr);
        channel = findKeyFormVal('platform', ponyArr);
        if (channel == 'taobao') {
            channel = '1';
        } else if (channel == '1688') {
            channel = '0';
        } else if (channel == 'weidian') {
            channel = '2';
        }
        return { id, channel };
    }


    try {
        const { result } = await resolveAddress({
            keyword: keyWords,
        });
        (id = result.spuNo), (channel = result.channel);
        return { id, channel };
    } catch (err) {
        console.log("err: ", err);
    }
}
// 特殊域名处理
export const searchSepUrl = async (keyWords) => {
    if (keyWords.indexOf("bilibili.com") !== -1 ||
        keyWords.indexOf("xiaohongshu.com") !== -1 ||
        keyWords.indexOf("goofish.com") !== -1 ||
        keyWords.indexOf("yupoo.com") !== -1 ||
        keyWords.indexOf("paipai.com") !== -1 ||
        keyWords.indexOf("jd.com") !== -1 ||
        keyWords.indexOf("dewu.com") !== -1 ||
        keyWords.indexOf("dw4.co") !== -1) {
        return keyWords;
    }
}